<template>
  <section class="todoapp">
    <!-- 使用组件 -->
    <TodoHeader :arr="list" @create="createFn"></TodoHeader>
    <TodoMain :arr="showArr" @del="deleFn"></TodoMain>
    <TodoFooter :farr="showArr" @changeType="changeFn" @clear="clearFun">
    </TodoFooter>
  </section>
</template>

<script>
// 1.创建组件文件,铺设标签样式
// 2.引入样式
import "./styles/base.css";
import "./styles/index.css";
// 3.引入组件文件
import TodoHeader from "./components/TodoHeader";
import TodoMain from "./components/TodoMain";
import TodoFooter from "./components/TodoFooter";
export default {
  // 注册组件
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  data() {
    return {
      list: JSON.parse(localStorage.getItem("todoList")) || [],
      // 6.5  定义一个中转变量用来接收传过来的类型字符串
      getSel: "all",
    };
  },
  methods: {
    createFn(taskName) {
      let id =
        this.list.length === 0 ? 100 : this.list[this.list.length - 1].id + 1;
      this.list.push({
        id: id,
        name: taskName,
        isDone: false,
      });
    },
    deleFn(i) {
      this.list.splice(i, 1);
    },
    changeFn(str) {
      // 6.6 将传递过来的字符串赋值给中转变量getSel
      this.getSel = str;
    },
    clearFun() {
      this.list = this.list.filter((obj) => obj.isDone === false);
    },
  },
  // 6.7 通过计算属性 定义一个新数组 配合list条件筛选显示出来
  computed: {
    showArr() {
      // 6.8 通过判断传递过来字符串 筛选出来的 显示数据
      if (this.getSel === "yes") {
        return this.list.filter((obj) => obj.isDone === true);
      } else if (this.getSel === "no") {
        return this.list.filter((obj) => obj.isDone === false);
      } else {
        return this.list;
      }
    },
  },
  // 8 数据缓存   深度侦听list数据
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todoList", JSON.stringify(this.list));
      },
    },
  },
};
</script>